import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
import net.miginfocom.swing.*;

/*
 * Created by JFormDesigner on Mon Dec 23 15:36:18 CST 2024
 */



/**
 * @author jjchen
 */
public class MF_bookmanager_deletebook extends JFrame {
    public MF_bookmanager_deletebook() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bookManagement mb = new bookManagement();
        mb.setVisible(true);    }

    private void button1MouseClicked2(MouseEvent e) {
        String a = textField1.getText();

        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String rootPassword = "root"; // 注意：不要在生产环境中使用硬编码的密码

        String sql = "DELETE from book_information where book_id = ?;";

        try (Connection conn = DriverManager.getConnection(url, username, rootPassword);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, a);

            int count = stmt.executeUpdate();
            System.out.println(count);
            JOptionPane.showMessageDialog(this, "删除图书成功！");
        } catch (SQLException ex) {
            // 处理SQL异常，例如记录日志或显示错误消息
            ex.printStackTrace();
        }    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label8 = new JLabel();
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label8 ----
        label8.setText("\u4fee\u6539\u4fe1\u606f");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 5f));
        contentPane.add(label8, "cell 5 1 1 2");

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\u8981\u5220\u9664\u7684id\uff1a");
        contentPane.add(label1, "cell 1 7");
        contentPane.add(textField1, "cell 4 7 6 1");

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u4fee\u6539");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
                button1MouseClicked2(e);
            }
        });
        contentPane.add(button1, "cell 5 16");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    public static void main(String[] args) {
        MF_bookmanager_deletebook mf = new MF_bookmanager_deletebook();
        mf.setVisible(true);
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label8;
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
